<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Buy;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;

class BuyController extends Controller
{
    protected $name = '购买记录';
    use ModelForm;
    
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Buy::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->like('user.mobile', '手机号');
                $filter->like('user.contact_name', '联系人');
                //$filter->addFilter($filter->like('post_title', '标题'));
            
            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('user.mobile', '手机号');
            $grid->column('user.contact_name', '联系人');
            $grid->column('money', '金额');
            
            $grid->column('item_type', '购买类型')->display(function($val){
                if($val == 'article')
				{
					return '报告下载';
				}else{
					if(isset($this->info)){
						return $this->info->name.'[名片]';
					}else{
						return '名片已经删除';
					}
				}	
				
            });
            
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Buy::class, function (Form $form) {
            
           
            
        });
    }
}
